// ProductCatalogManager.cpp: implementation of the ProductCatalogManager class.
//
//////////////////////////////////////////////////////////////////////

#include "StdAfx.h"
#include "ProductCatalogManager.h"
#include "DBConnectionManager.h"
#include "ProjectManager.h"
#include "PHSDirectorySetting.h"

using namespace Global;
using namespace DAWrapper;

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

namespace JointSystem
{
	
	
	ProductCatalogManager::ProductCatalogManager()
	{
		m_Conn = 0;
		m_nState = eNOREADALL;
	}
	
	ProductCatalogManager::~ProductCatalogManager()
	{
	}
	
	ProductCatalog* ProductCatalogManager::GetProductCatalog( const CString& productCatalogID )
	{
		ProductCatalog* pc = NULL;
		pc = this->GetManaged(productCatalogID);
		if(pc != NULL)
			return pc;
		
		CString strQuery;
		strQuery.Format( _T( "SELECT * FROM Product_Catalog WHERE ID=\'%s\'" ), productCatalogID );
		
		RecordsetPtrWrap rs;
		if ( FAILED( rs.Set( strQuery, *DataSourceConnection() ) ) ) return 0;
		if ( rs.GetRecordCount() == 0 ) return 0;
		
		return GetCatalog(rs);
	}
	
	_ConnectionPtr* ProductCatalogManager::DataSourceConnection()
	{
		if ( m_Conn ) return m_Conn;
		ASSERT(project());
		Project* pPrj = project();
		CString strSource = pPrj->ProjectPath() + pPrj->ProjectSource();
		ASSERT(!strSource.IsEmpty());

		m_Conn = DBConnectionManager::Instance()->GetConnectionPtrByUri( 
			strSource );
		return m_Conn;
	}
	
	ProductCatalogManager* ProductCatalogManager::Instance() 
	{ 
		ASSERT(GetProjectManager().CurProject());
		Project* pPrj = GetProjectManager().CurProject();

		return &pPrj->PCManager();
	}
	
	void ProductCatalogManager::DeleteInstance() 
	{ 
		//	if ( g_Instance ) 
		//		delete g_Instance, g_Instance = 0; 
	}
	
	BOOL ProductCatalogManager::GetAllCatalog()
	{
		if(m_nState == eREADALL)
			return TRUE;

		ProductCatalog* pCatalog = NULL;
		CString strSql;
		strSql.Format(_T("select * from Product_Catalog"));
		
		RecordsetPtrWrap rs;
		if ( FAILED( rs.Set( strSql, *DataSourceConnection() ) ) ) return 0;
		if ( rs.GetRecordCount() == 0 ) return 0;

		while(!rs.IsBOF() && !rs.IsEOF())
		{
			pCatalog = this->GetManaged(rs.GetString(_T("ID")));
			if(pCatalog != NULL)
			{
				rs.MoveNext();
				continue;
			}
			GetCatalog(rs);
			rs.MoveNext();
		}
		m_nState = eREADALL;
		return TRUE;
	}

	ProductCatalog* ProductCatalogManager::GetCatalog(RecordsetPtrWrap& rs)
	{
		ProductCatalog *pc = NULL;

		CString strQuery;

		pc = new ProductCatalog;
		pc->ID( rs.GetString(_T("ID") ));
		pc->Name( rs.GetString( _T( "Name" ) ) );
		pc->Description( rs.GetString( _T( "Desc" ) ) );
		strQuery = rs.GetString( _T( "Url" ) );
		strQuery.TrimRight();
		strQuery.TrimLeft();
		if(!strQuery.IsEmpty())
		{
			if ( strQuery.GetAt( strQuery.GetLength() - 1 ) != _T( '\\' ) )
				strQuery += _T( '\\' );
		}
		pc->Url( GetCurDirectorySetting().CrudeDBDir() + strQuery );
		pc->InfoTable(rs.GetString(_T("Info_Table")));
		pc->Style(rs.GetInt(_T("Style")));
		pc->DataSourceURI( rs.GetString( _T( "Data_Source_Name" ) ) );
		
		this->Add( pc->ID(), pc );
		return pc;
	}

	void ProductCatalogManager::GetProductCatalogNames( CStringArray& catalogNames, INT nStyle )
	{
		catalogNames.RemoveAll();
		GetAllCatalog();

		ProductCatalog* pCatalog = NULL;
		Iterator* pIterator = this->NewIterator();

		for(pIterator->Begin(); !pIterator->End(); pIterator->Next())
		{
			pCatalog = pIterator->Get();
			ASSERT(pCatalog);
			if(pCatalog->Style() == nStyle)
			{
				catalogNames.Add(pCatalog->Name());
			}
		}
		delete pIterator;
	}

	void ProductCatalogManager::WriteSelectStandard(CString &strID, ProductCatalog *pSource)
	{
		CString strSQL=_T("");
		this->Add(strID,pSource);
		_RecordsetPtr rs;
		rs.CreateInstance(__uuidof(Recordset));
		strSQL.Format("select * from Product_Catalog where ID='%s'",pSource->ID());
		try
		{
			rs->Open(_variant_t(strSQL),(IDispatch*)*DataSourceConnection(),adOpenStatic,adLockOptimistic,adCmdText);
			if(rs->GetRecordCount()==0)
			{
				rs->AddNew();
				rs->PutCollect(_T("Id"),_variant_t(pSource->ID()));
				rs->PutCollect(_T("style"),_variant_t((long)pSource->Style()));
				rs->PutCollect(_T("Info_Table"),_variant_t(pSource->InfoTable()));
				rs->PutCollect(_T("Name"),_variant_t(pSource->Name()));
				rs->PutCollect(_T("Data_source_name"),_variant_t(pSource->GetDSURI()));
				rs->Update();
			}
		}
		catch(_com_error e)
		{			
			ErrorAlert(e);
		}
	}
	
	void ProductCatalogManager::DeleteAll()
	{
		ManagerTemplate<CString, ProductCatalog>::DeleteAll();
		m_nState = eNOREADALL;
	}
}

